package java学习.反射;

import org.junit.Test;

import java.lang.annotation.Annotation;
import java.lang.reflect.AnnotatedType;
import java.lang.reflect.Constructor;
import java.lang.reflect.Field;
import java.lang.reflect.Method;

/**
 * **************
 * 项目名称: 蓝桥杯 <br/>
 * 文件名称:  <br/>
 * 文件描述: 这里添加您的类文件描述，说明当前文件要包含的功能。 <br/>
 * 文件创建：刘世锦 <br/>
 * 创建时间: 2022/4/11 <br/>
 *
 * @version v1.0 <br/>
 * @update [序号][日期YYYY-MM-DD][更改人姓名][变更描述]<br/>
 * *************
 */
public class TestReflection2 {

    public static void main(String[] args) throws Exception {
        Class<?> aClass = Class.forName("java学习.反射.Ref");
        String name = aClass.getName();
        Field[] fields = aClass.getFields();
        for (Field field : fields) {
            System.out.println("本类的所有public属性"+field);
        }
        Field[] declaredFields = aClass.getDeclaredFields();
        for (Field declaredField : declaredFields) {
            System.out.println("本类的所有属性name"+declaredField+"修饰符："+declaredField.getModifiers()+"---类型："+declaredField.getType());
        }
        Method[] methods = aClass.getMethods();
        for (Method method : methods) {
            System.out.println("本类及父类的public方法："+method);
        }

        Method[] declaredMethods = aClass.getDeclaredMethods();
        for (Method declaredMethod : declaredMethods) {
            System.out.println("本类的所有方法"+declaredMethod+"修饰符："+declaredMethod.getModifiers()+"方法的返回类型"+declaredMethod.getReturnType());
            Class<?>[] parameterTypes = declaredMethod.getParameterTypes();
            for (Class<?> parameterType : parameterTypes) {
                System.out.println("形参类型： "+parameterType);
            }
        }

        Constructor<?>[] constructors = aClass.getConstructors();
        for (Constructor<?> constructor : constructors) {
            System.out.println("本类的public构造器"+constructor);
        }
        Constructor<?>[] declaredConstructors = aClass.getDeclaredConstructors();
        for (Constructor<?> declaredConstructor : declaredConstructors) {
            System.out.println("本类的所有构造方法"+declaredConstructor+"修饰符："+declaredConstructor.getModifiers());

            Class<?>[] parameterTypes = declaredConstructor.getParameterTypes();
            for (Class<?> parameterType : parameterTypes) {
                System.out.println("形参类型"+parameterType);
            }

        }

        Class<?> superclass = aClass.getSuperclass();
        System.out.println("父类："+superclass);

        Class<?>[] interfaces = aClass.getInterfaces();
        for (Class<?> anInterface : interfaces) {
            System.out.println("该类的所有接口："+anInterface);
        }

        Annotation[] annotations = aClass.getAnnotations();
        for (Annotation annotation : annotations) {
            System.out.println("注解信息："+annotation);
        }

    }

    @Test
    public void testConstructObject() throws Exception {
        Class<?> aClass = Class.forName("java学习.反射.Ref");
        // 无参构造 实例化
        Object o = aClass.newInstance();
        System.out.println(o);
        // 有参构造 实例化
        Constructor<?> constructor = aClass.getConstructor(String.class);
        Object newInstance = constructor.newInstance("名字");
        System.out.println(newInstance);

//        有参构造 （私有）实例化：
        Constructor<?> declaredConstructor = aClass.getDeclaredConstructor(String.class, String.class, double.class);
        declaredConstructor.setAccessible(true);
        Object o1 = declaredConstructor.newInstance("刘", "jetry", 3.0);
        System.out.println(o1);

    }

}
interface XX{

}
@Deprecated
class Ref implements XX  {
    //属性
     public String name;
     protected static int age; // 4 + 8 = 12
     String job;
     private double sal;

    public Ref() {
    }

    public Ref(String name) {
        this.name = name;
    }

    private Ref(String name, String job, double sal) {
        this.name = name;
        this.job = job;
        this.sal = sal;
    }

    @Override
    public String toString() {
        return "Ref{" +
                "name='" + name + '\'' +
                ", job='" + job + '\'' +
                ", sal=" + sal +
                '}';
    }

//    方法
    public void m1(String name, int age, double sal) {

    }
    protected String m2() {
        return null;
    }
    String m3() {
        return null;
    }
    private void m4() {

    }
}